package com.managermype.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.managermype.dao.interfaces.UserDao;
import com.managermype.domain.User;


@SuppressWarnings({"rawtypes", "unchecked"})
@Repository
public class UserDaoHibernateImpl implements UserDao {

	@Resource
	private SessionFactory sessionFactory;

	@Override
	public User insert(User user) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		Integer idUser;
		try {
			tx = session.beginTransaction();
			idUser = (Integer) session.save(user);
			user.setIdUser(idUser);
			tx.commit();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		}
		finally {
			session.close();
		}
		return user;
	}

	@Override
	public void update(User user) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(user);
			tx.commit();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		}
		finally {
			session.close();
		}

	}

	@Override
	public User login(String username, String password) throws HibernateException {

		Session session = sessionFactory.openSession();
		Transaction tx = null;
		List result = null;
		try {
			tx = session.beginTransaction();
			Query query = session.getNamedQuery("loginUserSP");
			query.setParameter("username", username);
			query.setParameter("password", password);
			result = query.list();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		}
		finally {
			session.close();
		}
		return result.size() > 0 ? (User) result.get(0) : null;
	}
	/**
	@Override
	public User login(String username, String password) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		List<User> lstUsers;
		try{
			tx = session.beginTransaction();
			Query query = session.createQuery("from User where username = :username and password = :password");
			query.setParameter("username", username);
			query.setParameter("password", password);
			lstUsers = query.list();
		}catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		}
		finally {
			session.close();
		}
		return lstUsers.size() > 0?(User)lstUsers.get(0):null;		
	}**/
	

	@Override
	public User getById(Integer idUser) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		User user;
		try {
			tx = session.beginTransaction();
			user = (User) session.get(User.class, idUser);
			tx.commit();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		}
		finally {
			session.close();
		}
		return user;
	}

	@Override
	public List<User> listByName(String name) throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteById(Integer idUser) throws HibernateException {
		// TODO Auto-generated method stub

	}

	@Override
	public User getByUsername(String username) throws HibernateException {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		List<User> lstUsers;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from User where upper(username) = :username");
			query.setParameter("username", username);
			lstUsers = query.list();
			tx.commit();
		}
		catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		}
		finally {
			session.close();
		}
		return lstUsers.size()>0?(User)lstUsers.get(0):null;
	}

}
